package com.zhiwang.planet.mvvm.viewmodel

import android.graphics.Canvas
import android.graphics.Rect
import android.graphics.drawable.ColorDrawable
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.lifecycle.ViewModel
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.zhiwang.planet.R
import com.zhiwang.planet.di.ActivityScope
import com.zhiwang.planet.util.dp
import com.zhiwang.planet.util.dp2px
import javax.inject.Inject
import kotlin.reflect.jvm.internal.impl.load.java.lazy.ContextKt.child


@ActivityScope
class ItemDecorationViewModel @Inject constructor(context: AppCompatActivity) : ViewModel() {

    var spaceEqualItemDecoration: RecyclerView.ItemDecoration

    var gameListDecoration: RecyclerView.ItemDecoration

    var horizontalItemDecoration: RecyclerView.ItemDecoration

    val TAG = "ItemDecorationViewModel"

    init {

        gameListDecoration = object : DividerItemDecoration(context, VERTICAL) {

            init {

                setDrawable(ContextCompat.getDrawable(context, R.color.gameItemDivierColor)
                        ?: ColorDrawable())
            }


            override fun onDraw(canvas: Canvas, parent: RecyclerView, state: RecyclerView.State) {

                canvas.save()
                val childCount = parent.childCount
                for (i in 0 until childCount) {
                    val child = parent.getChildAt(i)
                    if (parent.getChildAdapterPosition(child) == parent.adapter?.itemCount?.minus(1) ?: -1)
                        continue

                    drawable?.setBounds(
                            dp2px(context, 145f),
                            child.bottom + 8.dp,
                            parent.right,
                            child.bottom + 8.dp + dp2px(context,
                                    1f))
                    drawable?.draw(canvas)
                }
                canvas.restore()
            }

            override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
                super.getItemOffsets(outRect, view, parent, state)

                val left = 0
                val top = dp2px(context, 8f)
                val right = 0
                val bottom = dp2px(context, 9f)
                outRect.set(left, top, right, bottom)
            }
        }

        horizontalItemDecoration = object : RecyclerView.ItemDecoration() {

            override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
                super.getItemOffsets(outRect, view, parent, state)
                val pos = parent.getChildAdapterPosition(view)
                val lastIndex = (parent.adapter?.itemCount ?: 1) - 1
                outRect.set(15.dp, 0.dp, if (pos == lastIndex) 15.dp else 0.dp, 0.dp)
            }
        }




        spaceEqualItemDecoration = object : RecyclerView.ItemDecoration() {

            var spanCount = 3
            var hSpace = 10.dp
            var vSpace = 10.dp

            var borderSpace = 12.5.dp

            override
            fun getItemOffsets(outRect: Rect, child: View, parent: RecyclerView, state: RecyclerView.State) {
                super.getItemOffsets(outRect, child, parent, state)

                val lm = parent.layoutManager as GridLayoutManager
                spanCount = lm.spanCount

                val spanSizeLookup = lm.spanSizeLookup

                val pos = parent.getChildAdapterPosition(child)
                if (spanSizeLookup.getSpanSize(pos) == spanCount) {
                    outRect.set(borderSpace, vSpace, borderSpace, 0)
                } else {

                    val col = spanSizeLookup.getSpanIndex(pos, spanCount)
                    when {
                        col % spanCount == 0 -> {

                            outRect.set(borderSpace, vSpace, 0, 0)
                        }
                        col % spanCount == spanCount - 1 -> {
                            outRect.set(hSpace, vSpace, borderSpace, 0)
                        }
                        else -> {
                            outRect.set(hSpace, vSpace, 0, 0)
                        }
                    }

                }

            }
        }


    }


}


